package com.huangmb.anymark.app.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.huangmb.androidlib.utils.Logger;
import com.huangmb.anymark.app.R;
import com.huangmb.anymark.app.activity.MarkerDetailActivity;
import com.huangmb.anymark.app.model.UserMarker;

import java.util.ArrayList;
import java.util.List;

/**
 * @author hmb
 * @version 1.0
 * @since 2016/4/16 13:49
 */
public class MarkerListAdapter extends RecyclerView.Adapter<MarkerListAdapter.PoiViewHolder> {
    private List<UserMarker> data = new ArrayList<>();
    private Context mContext;

    public MarkerListAdapter(Context context) {
        this.mContext = context;
    }

    public void setData(List<UserMarker> list) {
        this.data = list;
        notifyDataSetChanged();
    }

    @Override
    public PoiViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.poi_list_item, parent, false);
        PoiViewHolder holder = new PoiViewHolder(view,mContext);

        holder.mTitle = (TextView) view.findViewById(R.id.title);
        holder.mType = (TextView) view.findViewById(R.id.type);
        holder.mInfo = (TextView)view.findViewById(R.id.info);
        holder.mPosition = (TextView) view.findViewById(R.id.position);

        return holder;
    }

    @Override
    public void onBindViewHolder(PoiViewHolder holder, int position) {
        holder.setUpView(data.get(position));
        holder.mPosition.setText((position+1)+".");
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    public List<UserMarker> getData() {
        return data;
    }

    static class PoiViewHolder extends RecyclerView.ViewHolder {

        private TextView mTitle;
        private TextView mType;
        private TextView mInfo;
        private TextView mPosition;
        private Context mContext;

        public PoiViewHolder(View itemView) {
            super(itemView);
        }
        public PoiViewHolder(View view,Context context){
            this(view);
            mContext = context;
            /*Point p = ScreenUtils.getScreenWidthAndHeight(context);
            ViewGroup.LayoutParams params = itemView.getLayoutParams();
            params.width = p.x - ScreenUtils.px2dpInt(context,250);*/
        }

        public void setUpView(final UserMarker marker) {
            Logger.d("MarkerListAdapter",marker.getTitle());
            mTitle.setText(marker.getTitle());
            mType.setText(marker.getCategory().getName());
            mInfo.setText(marker.getInfo());
            mTitle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    MarkerDetailActivity.newInstance(mContext,marker);
                }
            });
        }
    }
}
